﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using PlanProd.Contracts.Misc;
using PlanProd.Contracts.enums;
using Web.Framework;
using PlanProd.Contracts;
using WebAppPlanProduccion.Code.Security;

namespace WebAppPlanProduccion.App
{
    public partial class PlanProduccionList : BasePage
    {       
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HasPermissionToSeeMe(PlanProdSecurity.PLANPRODUCCION_VER);

                if (!this.IsPostBack)
                {
                    UIHelper.LoadTemporadas(this.cboTemporada);
                    UIHelper.LoadDestinos(this.cboDestino, "Todos");
                    UIHelper.LoadProveedores(this.cboProveedor, "Todos");
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected override void ApplySecurity()
        {          
            if (this.GridPlanProduccion.DataSource != null)
            {
                this.GridPlanProduccion.Columns[0].Visible = HasPermissionFor(PlanProdSecurity.PLANPRODUCCION_VER);
               
            }
        }

        private void Buscar()
        {
            ServiceOptions searchOptions = new ServiceOptions() { PageSize = this.GridPlanProduccion.PageSize, PageNumber = this.GridPlanProduccion.PageIndex };
            int? planId = UIHelper.GetIntFromInputText(this.txtPlanId.Text);
            int? temporadaId = UIHelper.GetIntFromInputCbo(this.cboTemporada);
            int? destinoId = UIHelper.GetIntFromInputCbo(this.cboDestino);
            int? proveedorId = UIHelper.GetIntFromInputCbo(this.cboProveedor);
            DateTime? fechaDesde = UIHelper.GetDateTimeFromInputText(this.txtFechaDesde.Text);
            DateTime? fechaHasta = UIHelper.GetDateTimeFromInputText(this.txtFechaHasta.Text);

            this.GridPlanProduccion.DataSource = WebServiceHelper.GetPlanProduccion(searchOptions, planId, temporadaId, destinoId, proveedorId, fechaDesde, fechaHasta);
            this.GridPlanProduccion.DataBind();
        }

        protected void GridPlanProduccion_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.GridPlanProduccion.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridPlanProduccion_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    int planProduccionId = Convert.ToInt32(this.GridPlanProduccion.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    this.Response.Redirect(string.Format("/App/PlanProduccionEdit.aspx?Id={0}", planProduccionId), true);
                }               
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridPlanProduccion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                PlanProduccion presupuestoProduccion = (PlanProduccion)e.Row.DataItem;
                e.Row.Cells[0].Attributes.Add("title", "Editar");               
            }
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }      
    }
}